December 2017 - Git, R Style Guide

Susan VanderPlas
2017-12-04

Progress Report

How's it going so far?

As of Monday, 12 people have completed Course 1 and one person has completed Course 2.

Average progress on Course 2 is 50.6%.

Progress Report

How's it going so far?

As of Monday, 12 people have completed Course 1 and one person has completed Course 2.

Average progress on Course 2 is 50.6%.

What would make this experience better/easier next time?

(Vulcan mind meld isn't an option, unfortunately)

Outline

  • Git vs. GitHub vs. Git BASH
  • Git, GitHub, and RStudio
  • Q&A
  • R Coding Style Conventions

Git

xkcd on git

Git Tools

Hierarchy of git tools

Using Git and GitHub

Create a repo on GitHub

Using Git and GitHub

Clone the repo to your computer

Using Git and GitHub

Make some changes

Using Git and GitHub

Add changed files to git

Using Git and GitHub

Commit your changes

Using Git and GitHub

Push changes to GitHub

Using Git and GitHub

Clone the repo to a different computer

Using Git and GitHub

Make changes and commit them

Using Git and GitHub

Push changes to GitHub

Using Git and GitHub

Pull changes from GitHub

Git in RStudio

Git in RStudio - Clone a Repo

Clone a Repository in RStudio

Git in RStudio - Clone a Repo

Clone a Repository in RStudio

Git in RStudio - Clone a Repo

Clone a Repository in RStudio

Git in RStudio - Clone a Repo

Clone a Repository in RStudio

Git in RStudio - Clone a Repo

Clone a Repository in RStudio

Git in RStudio - Clone a Repo

Clone a Repository in RStudio

Git in RStudio - Clone a Repo

Clone a Repository in RStudio

Git in RStudio - RStudio Project

Clone a Repository in RStudio

Git in RStudio - Make Changes

Clone a Repository in RStudio

Git in RStudio - Stage Changes

Clone a Repository in RStudio

Git in RStudio - Commit Changes

Clone a Repository in RStudio

Git in RStudio - Commit Message

Clone a Repository in RStudio

Git in RStudio - Commit Changes

Clone a Repository in RStudio

Git in RStudio - Push

Clone a Repository in RStudio

Git in RStudio - Push (alt version)

Clone a Repository in RStudio

Git in RStudio - Push

Clone a Repository in RStudio

Git in RStudio - Push complete!

Clone a Repository in RStudio

Git in RStudio - History

Clone a Repository in RStudio

Git in RStudio - History

Clone a Repository in RStudio

Git in RStudio - Pull

Clone a Repository in RStudio

Git in RStudio - Tips

  • When you open a project, pull before you make any changes
  • One commit for each incremental improvement or set of changes that accomplish a single goal
  • Push often
  • Pull often (if multiple computers or collaborators)
  • google any error messages you get!
    GitHub has great documentation!

Git Guides

Questions

What isn't clear about the current material?

R Coding Style

A set of conventions for programming that make code easily readable and sharable

NPPD will use a modified form of Google's style guide

R Coding Style

  • File Names end in .R
  • Variables: variableName … should be descriptive
  • Functions: function_name or FunctionName
  • Lines less than 80 characters long (continue command on next line)
  • Indent: use 2 spaces (no tabs)
    Tools -> Global Options -> Code in RStudio to change
  • Assignment: Use <-. Do not use = or ->.
    = is used inside function calls - this is ok

R Coding Style

  • Curly Braces: first on same line, last on own line. Space between ) and {
my_fun <- function(x) {
  return(x)
}

R Coding Style

  • else: Surround with braces
x <- 3

if (x > 2) {
  y <- 4
} else {
  y <- 2
}

R Coding Style

File Layout:

  • Author
  • File Description
  • Library statements
  • Function Definitions
  • Executed Statements

R Coding Style

# Author: Susan Vanderplas

# ------------------------------------------------------
# This file demonstrates the ideal layout of an R code 
# file. This block is for a description of the code's
# purpose and file contents. 
# ------------------------------------------------------

# --- Packages -----------------------------------------
library(ggplot2)
library(dplyr)
# ------------------------------------------------------

R Coding Style

myfun <- function(x) {
  # This function returns the value that is passed in
  # Basically, it does nothing.
  # Args:
  #   x: The value to be returned
  # Returns: 
  #   x: The value passed in to the function

  return(x)
}

# This code executes myfun on the value 3
myfun(3)

R Coding Style

  • Errors: stop() or stopifnot() should be used to generate errors
  • Warnings: warning("Message goes here") should be used to generate warnings
  • Messages: message("Message goes here") should be used to generate messages.
    • Debugging messages are useful, but should be commented out when code is ready for production.

It will be ok!

Eye-bleach